package com.cskaoyan.service.back;

import com.cskaoyan.bean.po.BasePageInfo;
import com.cskaoyan.bean.po.CommonData;
import com.cskaoyan.bean.po.MarketComment;
import com.cskaoyan.bean.po.MarketCommentExample;
import com.cskaoyan.bean.vo.back.CommentVO;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @author: jzc
 * @since: 2022/10/12 21:05
 **/
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    MarketCommentMapper marketCommentMapper;
    @Override
    public CommonData getCommentList(BasePageInfo basePageInfo,Integer userId,Integer valueId) {
        CommonData<MarketComment> commonData = new CommonData<>();
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        marketCommentExample.setOrderByClause(basePageInfo.getSort()+" "+basePageInfo.getOrder());
        if (userId != null&&valueId!=null) {
            MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
            criteria.andUserIdEqualTo(userId);
            criteria.andValueIdEqualTo(valueId);
        }
        if (userId != null) {
            MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null) {
            MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
            criteria.andValueIdEqualTo(valueId);
        }

        List<MarketComment> marketComments = marketCommentMapper.selectByExample(marketCommentExample);
        PageInfo<MarketComment> marketCommentPageInfo = new PageInfo<>(marketComments);
        long CommentCount = marketCommentMapper.countByExample(marketCommentExample);
        commonData.setTotal((int) CommentCount);
        commonData.setPages(marketCommentPageInfo.getPages());
        commonData.setLimit(basePageInfo.getLimit());
        commonData.setPage(basePageInfo.getPage());
        commonData.setList(marketCommentPageInfo.getList());

        return commonData;
    }

    @Override
    public Integer deleteComment(MarketComment marketComment) {
        int effected = marketCommentMapper.deleteByPrimaryKey(marketComment.getId());
        return effected;
    }
}
